package models

import (
	"github.com/beego/beego/v2/client/orm"
	"weiboxs.com/new_h_glasses/utils"
)

type ErpBadreportData struct {
	Id             int64
	Badreport      *ErpBadreport `orm:"rel(fk);null;on_delete(do_nothing)"`
	Inventory      *ErpInventory `orm:"rel(fk);null;on_delete(do_nothing)"`
	BadreportReson string        `json:"badreport_reson"`
	BadreportNum   int64         `json:"badreport_num"`
}

func (a *ErpBadreportData) TableName() string {
	return ErpBadreportDataTableName()
}
func ErpBadreportDataTableName() string {
	return TableName("erp_badreport_data")
}

//查询
type ErpBadreportDataQueryParam struct {
	BaseQueryParam
	Badreport    int64  `json:"badreport_id"`
	Number       string `json:"badreport_number"`
	DepartmentId int64  `json:"depid"`
	StartDate    string `json:"start_date"`
	EndDate      string `json:"end_date"`
	Auditing     int    `json:"auditing"`
	RelateSel    []interface{}
}

func ErpBadreportDataPageList(params *ErpBadreportDataQueryParam) ([]*ErpBadreportData, int64) {

	ls := make([]*ErpBadreportData, 0)
	query := orm.NewOrm().QueryTable(ErpBadreportDataTableName())
	if len(params.RelateSel) > 0 {
		query = query.RelatedSel(params.RelateSel...)
	}
	if params.Badreport > 0 {
		query = query.Filter("Badreport__id", params.Badreport)
	}

	if len(params.Number) > 0 {
		query = query.Filter("Badreport__Number", params.Number)
	}
	if len(params.StartDate) > 0 {
		query = query.Filter("Badreport__BadreportTime__gte", utils.GetDateParse(params.StartDate))
	}
	if len(params.EndDate) > 0 {
		query = query.Filter("Badreport__BadreportTime__lte", utils.GetDateParse(params.EndDate)+68400)
	}
	if params.DepartmentId > 0 {
		query = query.Filter("Badreport__department_id", params.DepartmentId)
	}
	if params.Auditing > 0 {
		query = query.Filter("Badreport__Auditing", params.Auditing)
	}

	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}

	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&ls)

	return ls, total
}
